package admin_system

import (
	"fmt"
	utils "hft/Http/Utils"
	redis_modules "hft/db/redis/modules"

	"github.com/gin-gonic/gin"
)

type IpParams struct {
	Ip string `json:"ip"`
}

// 获取IP列表
type Ip struct{}

func (*Ip) List(c *gin.Context) {
	list, err := redis_modules.IpCacheDB.IpList()
	var newList = make([]IpParams, 0)
	for _, v := range list {
		newList = append(newList, IpParams{Ip: v})
	}
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	fmt.Println("hahhahah", newList)
	utils.OkWithData(newList, c)
}

func (*Ip) AddIp(c *gin.Context) {
	var data *IpParams
	c.ShouldBindJSON(&data)
	if err := redis_modules.IpCacheDB.AddIp(data.Ip); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}
}

func (*Ip) DeleteIp(c *gin.Context) {
	var data *IpParams
	c.ShouldBindJSON(&data)
	if err := redis_modules.IpCacheDB.DeleteIp(data.Ip); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}
}

var IpController = new(Ip)
